package com.icangya.DAO.impl;

import com.icangya.DAO.IFbUserMissionDAO;
import com.icangya.domain.FbUserMission;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Repository
public class FbUserMissionDAOImpl extends  BaseDAO<FbUserMission> implements IFbUserMissionDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<FbUserMission> getFbUserMission() {
//        String hql="FROM FbUserMission um where um.status=1 and  to_days(um.createTime)= to_days(?)";
        String hql="FROM FbUserMission um where um.status=1 ";
        return (List<FbUserMission>) getHibernateTemplate().find(hql);
    }

    @Override
    public FbUserMission getUserMissionByMissionId(long missionId) {
        String hql="FROM FbUserMission um where um.missionId=?";
        List list=getHibernateTemplate().find(hql,missionId);
        if(list.size()>0)
        return (FbUserMission) list.get(0);
        return null;
    }

    @Override
    public FbUserMission getUserMissByMissIdOrOpenId(String openId, long missionId) {
        String hql="FROM FbUserMission um where um.missionId=? and um.openId=?";
        List list=getHibernateTemplate().find(hql,missionId,openId);
        if(list.size()>0)
             return (FbUserMission) list.get(0);

        return null;
    }

    @Override
    public List<FbUserMission> getFbUserMissionNow() {
        String hql="FROM FbUserMission mr where to_days(mr.createTime)= to_days(?)";
        return (List<FbUserMission>) getHibernateTemplate().find(hql,new Timestamp(new Date().getTime()));
    }

    @Override
    public Object[] getUserMissionIdNow(long missionId) {
        String hql="select um.id,um.createTime,um.status,um.missionId,um.missionName,um.missionContent from FbUserMission um,FbMissionRecord mr where  um.missionId=mr.id  and mr.missionId=? " +
                "and um.status=1 and TO_DAYS(um.createTime)=TO_DAYS(?)";
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        Query query=  session.createQuery(hql);
        query.setParameter(0,missionId);
        query.setParameter(1,new Timestamp(new Date().getTime()));
        List<Object[]> list= query.list();
        if(list.size()>0)
            return list.get(0);
        return null;
    }
}






















